<?php
/**
 * Pacif : PHP Awesome Configuration file InterFace
 * 
 * @version 0.0.1
 * @author YAGI.Teruo <terurou@gmail.com>
 * @copyright 2008 YAGI.Teruo
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
 */
class Pacif
{
    /**
     * Load a configuration file into array.
     *
     * In configuration file, you cannot define the vars named '$___configure_path___',
     * '$___before_define_vars___' and '$___after_define_vars___'.
     * 
     * @param string $___configure_path___ configuration file path
     * @return array
     * @access public
     * @static
     */
    function load($path) {
        $instance = Pacif::_getInstance();
        return $instance->_load($path);
    }

    /**
     * Dump array to PHP code string.
     * 
     * @param array $array PHP array
     * @return string
     * @access public
     * @static
     */
    function dump($array) {
        $instance = Pacif::_getInstance();
        return $instance->_dump($array);
    }

    /**
     * @access protected
     */
    function _getInstance() {
        static $instance;
        if ($instance == null) {
            $instance = new Pacif();
        }
        return $instance;
    }

    /**
     * @access protected
     */
    function _load($___configure_path___)
    {
        $___before_define_vars___ = get_defined_vars();
        require($___configure_path___);
        $___after_define_vars___ = get_defined_vars();
        unset($___after_define_vars___['___before_define_vars___']);
        foreach(array_keys($___before_define_vars___) as $key) {
            unset($___after_define_vars___[$key]);
        }
        return $___after_define_vars___;
    }

    /**
     * @access protected
     */
    function _dump($array)
    {
        $buffer = '';
        foreach ($array as $key => $val) {
            if (is_array($val)) {
                $buffer .= '$'.$this->_escapeBlank($key).' = '.$this->_serializeArray($val, 0).";\n";
            } else {
                $buffer .= '$'.$this->_escapeBlank($key).' = '.$this->_serializeVal($val, 0).";\n";
            }
        }
        return "<?php\n" . $buffer . "?>";
    }

    /**
     * @access protected
     */
    function _escapeBlank($string) {
        return str_replace(array(' ', "\t"), '_', $string);
    }

    /**
     * @access protected
     */
    function _serializeArray($input, $indent)
    {
        $tmpArray = array();
        foreach ($input as $key => $val) {
            $pre = "\n".str_repeat(' ', $indent+4)
                    . (is_numeric($key) ? $key : "'$key'") . ' => ';
            if(is_array($val)) {
                $tmpArray[] = $pre.$this->_serializeArray($val, $indent+4);
            } else {
                $tmpArray[] = $pre.$this->_serializeVal($val);
            }
        }
        return "array(".implode(",", $tmpArray)."\n".str_repeat(' ', $indent).")";
    }

    /**
     * @access protected
     */
    function _serializeVal($val) 
    {
        if (is_string($val)) {
            return '"'.str_replace("\r", '\r', str_replace("\n", '\n', $val)).'"';
        } elseif($val === null) {
            return 'null';
        } else {
            return $val;
        }
    }
}
?>